package summary;

/**
 * @Author: 海琳琦
 * @Date: 2022/3/9 21:50
 * https://leetcode-cn.com/problems/add-binary/
 */
public class Title67 {

    public static String addBinary(String a, String b) {
        int n = a.length();
        int m = b.length();
        int[] result = new int[n + m];
        int count = 0;
        for (int i = a.length() - 1, j = b.length() - 1; i >= 0 || j >= 0; i--,j--) {
            int x,y;
            if (i < 0) {
                x = 0;
            }else{
                x = a.charAt(i) - '0';
            }
            if (j < 0) {
                y = 0;
            }else{
                y = b.charAt(j) - '0';
            }
            result[count++] = x + y;
        }
        for (int i = 1; i < result.length; i++) {
            result[i] += result[i - 1] / 2;
            result[i - 1] %= 2;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = result.length - 1; i >= 0; i--) {
            if (result[i] == 0 && sb.length() == 0) {
                continue;
            }
            sb.append(result[i]);
        }
        if (sb.length() == 0) {
            return "0";
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        addBinary("0", "0");
    }
}
